/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-05-31 13:44:24
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2023-06-25 14:50:06
 * @FilePath: \greenbeeadmin\controllers\admin\service\userService.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package service

import (
	"fmt"
	"greenbeeadmin/models"

	"github.com/beego/beego/v2/client/orm"
)

/**
 * @description: 系统设置 service
 * @return {*}
 */
type SystemSetService struct {
	AdminBaseService
}

/**
 * @description: 添加
 * @param {models.SystemSetting} data
 * @return {*}
 */
func (s *SystemSetService) Add(data models.SystemSetting) int64 {
	o := orm.NewOrm()
	id, err := o.Insert(&data)
	if err == nil {
		//fmt.Println(id)
	}
	return id
}

/**
 * @description: 编辑
 * @param {models.SystemSetting} data
 * @return {*}
 */
func (s *SystemSetService) Edit(data models.SystemSetting, uid int, field []string) int64 {
	o := orm.NewOrm()
	user := models.SystemSetting{Id: uid}
	if o.Read(&user, "Id") == nil {
		data.Id = uid
		if num, err := o.Update(&data, field...); err == nil {
			fmt.Println(num)
			return num
		}
	}
	return 0
}

/**
 * @description: 获取数据
 * @param {models.SystemSetting} data
 * @param {[]string} field
 * @return {*}
 */
func (s *SystemSetService) GetData(data models.SystemSetting, field []string) (models.SystemSetting, error) {
	o := orm.NewOrm()
	err := o.Read(&data, field...)
	if err == orm.ErrNoRows {
		fmt.Println("查询不到")
	} else if err == orm.ErrMissPK {
		fmt.Println("找不到主键")
	}
	return data, err
}
